package mr.fresh.manage.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mr.fresh.api.CommonResult;
import mr.fresh.api.controller.manaage.good.FreshGoodsControllerApi;
import mr.fresh.entity.request.FindListGoodRequest;
import mr.fresh.entity.request.SaveGoodRequest;
import mr.fresh.entity.response.FindListGoodResponse;
import mr.fresh.serivce.manage.good.FreshGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @CLassName FreshProductControllerApiImpl
 * @Description 商品接口
 * @Author mr.jl
 * @Date 2019/5/27 21:55
 * @Version 1.0
 **/

@RestController
@RequestMapping("/goods")
@Api(tags = "FreshGoodsControllerApiImpl", description = "商品管理")
public class FreshGoodsControllerApiImpl implements FreshGoodsControllerApi {

    @Autowired
    private FreshGoodsService goodsService;

    /**
     * @Description: 商品查询接口
     * @Param: []
     * @return: mr.fresh.api.CommonResult
     * @Author: Mr.jl
     * @Date: 2019/5/27
     */
    @Override
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "商品查询接口", notes = "商品查询接口", httpMethod = "POST")
    public CommonResult findlist(@ApiParam(name = "FindListGoodRequest", value = "商品查询模型", required = true) @RequestBody FindListGoodRequest model) {
        return CommonResult.success(goodsService.findList(model));
    }


    /**
     * @Description: 商品创建和更新接口
     * @Param: [model]
     * @return: mr.fresh.api.CommonResult
     * @Author: Mr.jl
     * @Date: 2019/6/8
     */
    @Override
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "新建或者更新商品接口", notes = "新建或者更新商品接口", httpMethod = "POST")
    public CommonResult saveGood(@ApiParam(name = "SaveGoodRequest", value = "商品基本信息", required = true) @Validated @RequestBody SaveGoodRequest model) {
        return model.getGoodId() == null ? goodsService.saveGoods(model) : goodsService.updateGoods(model);
    }

    /**
     * @Description:
     * @Param: [id]
     * @return: mr.fresh.api.CommonResult
     * @Author: Mr.jl
     * @Date: 2019/6/8
     */
    @Override
    @RequestMapping(value = "findById", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询数据", notes = "根据id查询数据", httpMethod = "GET")
    public CommonResult findById(@RequestParam(value = "id", required = true) @ApiParam(value = "商品id") Long id) {
        FindListGoodResponse response = goodsService.findById(id);
        return response != null ? CommonResult.success(response) : CommonResult.failed("没有这条记录，请检查参数");

    }

    /**
     * @Description: 根据id删除商品信息
     * @Param: [id]
     * @return: mr.fresh.api.CommonResult
     * @Author: Mr.jl
     * @Date: 2019/6/8
     */
    @Override
    @RequestMapping(value = "deleteById", method = RequestMethod.GET)
    @ApiOperation(value = "根据id删除数据", notes = "根据id删除数据", httpMethod = "GET")
    public CommonResult deleteById(@RequestParam(value = "id", required = true) @ApiParam(value = "商品id") Long id) {
        Integer integer = goodsService.deleteById(id);
        return integer != null && integer > 0 ? CommonResult.success() : CommonResult.failed();
    }
}
